package com.anthill.utils
{
import com.anthill.handlers.EventHandler;

import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.utils.describeType;

/**
 *  Utill calss for working with metadata.
 * 
 */ 
public class MetadataUtil extends EventDispatcher
{
	/**
     *  Constructor. 
     *  
     */ 
	public function MetadataUtil()
	{
	}
	
	/**
	 *  Static function that parse objects and returns array of event 
	 *  dispathers. It uses AnthillEventHandler metadata.
	 * 
	 *  @param object IEventDispatcher that needs to be parsed (Managers or Views).
	 * 
	 *  @return Array of <code>EventDispacth</code> objects.
	 */			
	static public function getEventHandlers(object:IEventDispatcher):Array
	{
		var eventHandlers:Array = [];
		
		var description:XML = describeType(object);
		for each (var xml:XML in description.method.metadata.(@name=="AnthillEventHandler"))
		{
			// create and fill in event handler
			var handler:EventHandler = new EventHandler();
			handler.type = xml.arg.(@key=="type").@value;
			
			var useCapture:String = xml.arg.(@key=="useCapture").@value; 
			handler.useCapture = useCapture != "" ? Boolean(useCapture) : null; 
			
			var useWeakReference:String = xml.arg.(@key=="useWeakReference").@value; 
			handler.useWeakReference = useWeakReference != "" ? Boolean(useWeakReference) : null;
			
			var listener:String = xml.parent().@name;
			if(listener != "" && object[listener] != null)
			{
				handler.listener = object[listener];
				// push event handler into Array
				eventHandlers.push(handler);
			}
		}	
		return eventHandlers;			
	}
}
}